﻿IF OBJECT_ID('up_MapHtml_AssetXmissionStructureSelectClosest') IS not NULL
DROP PROC up_MapHtml_AssetXmissionStructureSelectClosest

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'up_MapHtml_AssetXmissionStructureSelectClosest')
	DROP PROCEDURE up_MapHtml_AssetXmissionStructureSelectClosest
GO


CREATE PROCEDURE [dbo].[up_MapHtml_AssetXmissionStructureSelectClosest] 
(
	@latitude	float,
	@longitude	float,
	@key		int
)
AS

DECLARE @IdList varchar(MAX)
SET @IdList = ''

SELECT	
	@IdList = @IdList + CONVERT(varchar, Utility_Asset_XMissionStructureId) + ','
FROM	
	tb_Utility_Asset_XMissionStructures assets
WHERE	
	dbo.fn_DistanceBetween(assets.Latitude, assets.Longitude, @latitude, @longitude) < 2000
	AND assets.Utility_ProjectFK = @key

SELECT @IdList


